/**
 * @param {Function} func
 * @param {number} wait 防抖
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
	let timeout, args, context, timestamp, result

	const later = function () {
		// 据上一次触发时间间隔
		const last = +new Date() - timestamp

		// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
		if (last < wait && last > 0) {
			timeout = setTimeout(later, wait - last)
		} else {
			timeout = null
			// 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
			if (!immediate) {
				result = func.apply(context, args)
				if (!timeout) context = args = null
			}
		}
	}

	return function (...args) {
		context = this
		timestamp = +new Date()
		const callNow = immediate && !timeout
		// 如果延时不存在，重新设定延时
		if (!timeout) timeout = setTimeout(later, wait)
		if (callNow) {
			result = func.apply(context, args)
			context = args = null
		}

		return result
	}
}

// 对象转数组
export function objToarr(obj) {
	let arr = []
	Object.keys(obj).forEach((key) => {
		arr.push({
			name: key,
			value: obj[key]
		})
	})
	return arr
}
const utils = {
	//去空格
	trim: function (value) {
		return value.replace(/(^\s*)|(\s*$)/g, '')
	},
	//内容替换
	replaceAll: function (text, repstr, newstr) {
		return text.replace(new RegExp(repstr, 'gm'), newstr)
	},
	//格式化手机号码
	formatNumber: function (num) {
		return num.length === 11 ? num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : num
	},
	//金额格式化
	rmoney: function (money) {
		return parseFloat(money)
			.toFixed(2)
			.toString()
			.split('')
			.reverse()
			.join('')
			.replace(/(\d{3})/g, '$1,')
			.replace(/\,$/, '')
			.split('')
			.reverse()
			.join('')
	},
	//日期格式化
	formatDate: function (formatStr, fdate) {
		if (fdate) {
			if (~fdate.indexOf('.')) {
				fdate = fdate.substring(0, fdate.indexOf('.'))
			}
			fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/')
			var fTime,
				fStr = 'ymdhis'
			if (!formatStr) formatStr = 'y-m-d h:i:s'
			if (fdate) fTime = new Date(fdate)
			else fTime = new Date()
			var month = fTime.getMonth() + 1
			var day = fTime.getDate()
			var hours = fTime.getHours()
			var minu = fTime.getMinutes()
			var second = fTime.getSeconds()
			month = month < 10 ? '0' + month : month
			day = day < 10 ? '0' + day : day
			hours = hours < 10 ? '0' + hours : hours
			minu = minu < 10 ? '0' + minu : minu
			second = second < 10 ? '0' + second : second
			var formatArr = [
				fTime.getFullYear().toString(),
				month.toString(),
				day.toString(),
				hours.toString(),
				minu.toString(),
				second.toString()
			]
			for (var i = 0; i < formatArr.length; i++) {
				formatStr = formatStr.replace(fStr.charAt(i), formatArr[i])
			}
			return formatStr
		} else {
			return ''
		}
	},
	rgbToHex: function (r, g, b) {
		return '#' + utils.toHex(r) + utils.toHex(g) + utils.toHex(b)
	},
	toHex: function (n) {
		n = parseInt(n, 10)
		if (isNaN(n)) return '00'
		n = Math.max(0, Math.min(n, 255))
		return '0123456789ABCDEF'.charAt((n - (n % 16)) / 16) + '0123456789ABCDEF'.charAt(n % 16)
	},
	hexToRgb(hex) {
		let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
		return result
			? {
					r: parseInt(result[1], 16),
					g: parseInt(result[2], 16),
					b: parseInt(result[3], 16)
			  }
			: null
	}
}
// 风向电码工具类
export const WindUtil = () => {}

// 风向电码转风向名以及角度
WindUtil.codeToObj = (code) => {
	const baseDatas = {
		1: {
			direction: '东北',
			code: 1,
			rotate: 45
		},
		2: {
			direction: '东',
			code: 2,
			rotate: 90
		},
		3: {
			direction: '东南',
			code: 3,
			rotate: 135
		},
		4: {
			direction: '南',
			code: 4,
			rotate: 180
		},
		5: {
			direction: '西南',
			code: 5,
			rotate: 225
		},
		6: {
			direction: '西',
			code: 6,
			rotate: 270
		},
		7: {
			direction: '西北',
			code: 7,
			rotate: 315
		},
		8: {
			direction: '北',
			code: 8,
			rotate: 360
		},
		9: {
			direction: '不定',
			code: 9,
			rotate: 0
		}
	}
	if (baseDatas[code]) {
		return baseDatas[code]
	} else {
		return {
			direction: '',
			code: 0,
			rotate: 0
		}
	}
}
WindUtil.nameToDir = (name) => {
	let arr = [
		{
			direction: '东北',
			code: 1,
			rotate: 45
		},
		{
			direction: '东',
			code: 2,
			rotate: 90
		},
		{
			direction: '东南',
			code: 3,
			rotate: 135
		},
		{
			direction: '南',
			code: 4,
			rotate: 180
		},
		{
			direction: '西南',
			code: 5,
			rotate: 225
		},
		{
			direction: '西',
			code: 6,
			rotate: 270
		},
		{
			direction: '西北',
			code: 7,
			rotate: 315
		},
		{
			direction: '北',
			code: 8,
			rotate: 360
		},
		{
			direction: '不定',
			code: 9,
			rotate: 0
		}
	]
	let item = arr.find((item) => item.direction == name)
	if (item) {
		return item.rotate
	} else {
		return 0
	}
}

// 风向值转风向名称
WindUtil.findWind = (num) => {
	let windData = [
		{
			name: '北风',
			vuale: [0, 11.25]
		},
		{
			name: '北风',
			vuale: [348.76, 360]
		},
		{
			name: '东北东风',
			vuale: [11.26, 33.75]
		},
		{
			name: '东北风',
			vuale: [33.76, 56.25]
		},
		{
			name: '东东北风',
			vuale: [56.26, 78.75]
		},
		{
			name: '东风',
			vuale: [78.76, 101.25]
		},
		{
			name: '东东南风',
			vuale: [101.26, 123.75]
		},
		{
			name: '东南风',
			vuale: [123.76, 146.25]
		},
		{
			name: '南东南风',
			vuale: [146.26, 168.75]
		},
		{
			name: '南风',
			vuale: [168.76, 191.25]
		},

		{
			name: '南西南风',
			vuale: [191.26, 213.75]
		},
		{
			name: '西南风',
			vuale: [213.76, 236.25]
		},
		{
			name: '西西南风',
			vuale: [236.26, 258.75]
		},
		{
			name: '西风',
			vuale: [258.76, 281.25]
		},
		{
			name: '西西北风',
			vuale: [281.26, 303.75]
		},
		{
			name: '西北风',
			vuale: [303.76, 336.25]
		},
		{
			name: '北西北风',
			vuale: [336.26, 348.75]
		}
	]
	return windData.find((_) => num >= _.vuale[0] && num <= _.vuale[1]).name || '-'
}

// 风速转等级
WindUtil.getWindPowerLevel = (fs) => {
	if (fs >= 56.1) return 17
	else if (fs >= 51.0) return 16
	else if (fs >= 46.2) return 15
	else if (fs >= 41.5) return 14
	else if (fs >= 37.0) return 13
	else if (fs >= 32.7) return 12
	else if (fs >= 28.5) return 11
	else if (fs >= 24.5) return 10
	else if (fs >= 20.8) return 9
	else if (fs >= 17.2) return 8
	else if (fs >= 13.9) return 7
	else if (fs >= 10.8) return 6
	else if (fs >= 8.0) return 5
	else if (fs >= 5.5) return 4
	else if (fs >= 3.4) return 3
	else if (fs >= 1.6) return 2
	else if (fs >= 0.3) return 1
	return 0
}

export const toFormData = (values) => {
	const formData = new FormData()
	if (values instanceof Object) {
		Object.keys(values).forEach((key) => {
			formData.append(key, values[key])
		})
	}
	return formData
}

export const filter = (str) => {
	// 特殊字符转义
	str += '' // 隐式转换
	str = str.replace(/%/g, '%25')
	str = str.replace(/\+/g, '%2B')
	str = str.replace(/ /g, '%20')
	str = str.replace(/\//g, '%2F')
	str = str.replace(/\?/g, '%3F')
	str = str.replace(/&/g, '%26')
	str = str.replace(/\=/g, '%3D')
	str = str.replace(/#/g, '%23')
	return str
}

export const objToParams = (paramObj, isaddW = true) => {
	const sdata = []
	for (let attr in paramObj) {
		sdata.push(`${attr}=${filter(paramObj[attr])}`)
	}
	return isaddW ? '?' + sdata.join('&') : sdata.join('&')
}

// const getAddress = () => {
// 	wx.request({
// 		url: 'https://apis.map.qq.com/ws/geocoder/v1',
// 		data: {
// 			key: 'XENBZ-NF4RU-LMIVC-G7CWJ-ZMMIK-KOFVN',
// 			location: `${latitude},${longitude}`
// 		},
// 		success: (cityResult) => {
// 			this.setData({
// 				address: cityResult.data.result.address
// 			})
// 		},
// 		fail: () => {},
// 		complete: () => {}
// 	})
// }
export default {
	trim: utils.trim,
	replaceAll: utils.replaceAll,
	formatNumber: utils.formatNumber,
	rmoney: utils.rmoney,
	formatDate: utils.formatDate,
	rgbToHex: utils.rgbToHex,
	hexToRgb: utils.hexToRgb
}
// 风速转风向
export const getWindDirection = (degrees, fxCount = 8) => {
	let hour1WindDirection = ''
	if (fxCount === 16) {
		if (degrees === 360) {
			hour1WindDirection = '不定向'
		} else if (degrees >= 348.75 && degrees < 360) {
			hour1WindDirection = '北'
		} else if (degrees >= 0 && degrees <= 11.25) {
			hour1WindDirection = '北'
		} else if (degrees > 11.25 && degrees <= 33.75) {
			hour1WindDirection = '北东北'
		} else if (degrees > 33.75 && degrees <= 56.25) {
			hour1WindDirection = '东北'
		} else if (degrees > 56.25 && degrees <= 78.75) {
			hour1WindDirection = '东东北'
		} else if (degrees > 78.75 && degrees <= 101.25) {
			hour1WindDirection = '东'
		} else if (degrees > 101.25 && degrees <= 123.75) {
			hour1WindDirection = '东东南'
		} else if (degrees > 123.75 && degrees <= 146.25) {
			hour1WindDirection = '东南'
		} else if (degrees > 146.25 && degrees <= 168.75) {
			hour1WindDirection = '南东南'
		} else if (degrees > 168.75 && degrees <= 191.25) {
			hour1WindDirection = '南'
		} else if (degrees > 191.25 && degrees <= 213.75) {
			hour1WindDirection = '南西南'
		} else if (degrees > 213.75 && degrees <= 236.25) {
			hour1WindDirection = '西南'
		} else if (degrees > 236.25 && degrees <= 258.75) {
			hour1WindDirection = '西西南'
		} else if (degrees > 258.75 && degrees <= 281.25) {
			hour1WindDirection = '西'
		} else if (degrees > 281.25 && degrees <= 303.75) {
			hour1WindDirection = '西西北'
		} else if (degrees > 303.75 && degrees <= 326.25) {
			hour1WindDirection = '西北'
		} else if (degrees > 326.25 && degrees < 348.75) {
			hour1WindDirection = '北西北'
		}
	} else {
		if (degrees === 360) {
			hour1WindDirection = '不定向'
		} else if (360 - 22.5 <= degrees || degrees <= 22.5) {
			hour1WindDirection = '北'
		} else if (degrees > 22.5 && degrees <= 45 + 22.5) {
			hour1WindDirection = '东北'
		} else if (90 - 22.5 < degrees && degrees <= 90 + 22.5) {
			hour1WindDirection = '东'
		} else if (135 - 22.5 < degrees && degrees <= 135 + 22.5) {
			hour1WindDirection = '东南'
		} else if (180 - 22.5 < degrees && degrees <= 180 + 22.5) {
			hour1WindDirection = '南'
		} else if (225 - 22.5 < degrees && degrees <= 225 + 22.5) {
			hour1WindDirection = '西南'
		} else if (270 - 22.5 < degrees && degrees <= 270 + 22.5) {
			hour1WindDirection = '西'
		} else if (315 - 22.5 < degrees && degrees <= 315 + 22.5) {
			hour1WindDirection = '西北'
		}
	}
	return hour1WindDirection
}

// 天气编码转换中文
export const getWeatherCode = (weatherCode) => {
	let weatherStr = ''
	switch (weatherCode) {
		case 0:
			weatherStr = '晴'
			break
		case 1:
			weatherStr = '多云'
			break
		case 2:
			weatherStr = '阴'
			break
		case 3:
			weatherStr = '阵雨'
			break

		case 4:
			weatherStr = '雷阵雨'
			break
		case 5:
			weatherStr = '雷阵雨伴有冰雹'
			break
		case 6:
			weatherStr = '雨夹雪'
			break
		case 7:
			weatherStr = '小雨'
			break
		case 8:
			weatherStr = '中雨'
			break
		case 9:
			weatherStr = '大雨'
			break
		case 10:
			weatherStr = '暴雨'
			break
		case 11:
			weatherStr = '大暴雨'
			break
		case 12:
			weatherStr = '特大暴雨'
			break
		case 13:
			weatherStr = '阵雪'
			break
		case 14:
			weatherStr = '小雪'
			break
		case 15:
			weatherStr = '中雪'
			break
		case 16:
			weatherStr = '大雪'
			break
		case 17:
			weatherStr = '暴雪'
			break
		case 18:
			weatherStr = '雾'
			break
		case 19:
			weatherStr = '冻雨'
			break
		case 20:
			weatherStr = '沙尘暴'
			break
		case 21:
			weatherStr = '小到中雨'
			break
		case 22:
			weatherStr = '中到大雨'
			break
		case 23:
			weatherStr = '大到暴雨'
			break
		case 24:
			weatherStr = '暴雨到大暴雨'
			break
		case 25:
			weatherStr = '大暴雨到特大暴雨'
			break
		case 26:
			weatherStr = '小到中雪'
			break
		case 27:
			weatherStr = '中到大雪'
			break
		case 28:
			weatherStr = '大到暴雪'
			break
		case 29:
			weatherStr = '浮尘'
			break
		case 30:
			weatherStr = '扬沙'
			break
		case 31:
			weatherStr = '强沙尘暴'
			break
		case 53:
			weatherStr = '霾'
			break
		case 99:
			weatherStr = '无'
			break
		default:
			weatherStr = '-'
			break
	}

	return weatherStr
}

// 风速转级别
export const getWindPowerLevel = (fs) => {
	if (fs >= 56.1) return 17
	else if (fs >= 51.0) return 16
	else if (fs >= 46.2) return 15
	else if (fs >= 41.5) return 14
	else if (fs >= 37.0) return 13
	else if (fs >= 32.7) return 12
	else if (fs >= 28.5) return 11
	else if (fs >= 24.5) return 10
	else if (fs >= 20.8) return 9
	else if (fs >= 17.2) return 8
	else if (fs >= 13.9) return 7
	else if (fs >= 10.8) return 6
	else if (fs >= 8.0) return 5
	else if (fs >= 5.5) return 4
	else if (fs >= 3.4) return 3
	else if (fs >= 1.6) return 2
	else if (fs >= 0.3) return 1
	return 0
}
